//Copyright 2008 by Wes Corp for Trafficman  All rights reserved.abstract 
package org.trafficman.trafficman_main;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import org.trafficman.trafficman_main.TrafficmanScreens;

public class TrafficmanFilesNoteEdit extends Activity {

    private EditText mTitleText;
    private Long mRowId;
    private TrafficmanFilesNotesDbAdapter mDbHelper;
    private int int_intent_active;
    private int jv_selected_file_id;
    private String title_text;
    private long record_id;
    private String datetime_record_updated;
    private String datetime_record_created;
    private TrafficmanScreens mScreens;
    private String jv_filename;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Window w = getWindow();
        w.requestFeature(Window.FEATURE_LEFT_ICON);

        setContentView(R.layout.files_note_edit);

        // get the xml layout page
        LinearLayout tmpLayout = (LinearLayout) findViewById(R.id.linearlayout_001);
        TrafficmanConstants.SCREEN_VIEWWIDTH = this.getResources().getDisplayMetrics().widthPixels;
        TrafficmanConstants.SCREEN_VIEWHEIGHT = this.getResources().getDisplayMetrics().heightPixels;
        get_JobView_Screen(); // set JobView2 background

        int resID = getResources().getIdentifier(TrafficmanConstants.SCREEN_NAME, null,
                null);
        tmpLayout.setBackground(resID);

        mTitleText = (EditText) findViewById(R.id.title);
        //mBodyText = (EditText) findViewById(R.id.body);

        Button confirmButton = (Button) findViewById(R.id.confirm);
        Button cancelButton = (Button) findViewById(R.id.cancel);

        mRowId = icicle != null ? icicle.getLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID) : null;
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID) : null;
        }

        populateFields();

        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                int_intent_active = 1;
                TrafficmanConstants.SAVE_CLICK = 1;
                setResult(RESULT_OK);
                Intent intent = new Intent();
                intent.setClass(TrafficmanFilesNoteEdit.this,
                        TrafficmanMainActivity.class);
                startActivity(intent);
                finish();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                // Constants.CANCEL_CLICK = 1;
                setResult(RESULT_CANCELED);
                Intent intent = new Intent();
                intent.setClass(TrafficmanFilesNoteEdit.this,
                        TrafficmanMainActivity.class);
                startActivity(intent);
                TrafficmanConstants.CANCEL_CLICK = 0;
                finish();
            }
        });

        // setup title bar
        title_text = getString(R.string.trafficman_files_edit_title);
        this.setTitle(title_text);
        w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
                R.drawable.tm_icon);

    }

    private void get_JobView_Screen() {
        mScreens = new TrafficmanScreens(this);
    }

    private void populateFields() {
        if (mRowId != null) {
            mDbHelper = new TrafficmanFilesNotesDbAdapter(this);
            mDbHelper.open();

            Cursor note = mDbHelper.fetchNote(mRowId);
            startManagingCursor(note);
            mTitleText.setText(note.getString(note.getColumnIndex(TrafficmanFilesNotesDbAdapter.KEY_JV_TITLE)));

            TrafficmanConstants.DATETIME_RECORD_CREATED = note.getString(note.getColumnIndex(TrafficmanFilesNotesDbAdapter.KEY_JV_DATETIME_CREATED));

            mDbHelper.close();
        }
    }

    @Override
    protected void onFreeze(Bundle outState) {
        super.onFreeze(outState);
        outState.putLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID, mRowId);
    }

    @Override
    protected void onPause() {
        if (int_intent_active == 1) {
            // do nothing
            int_intent_active = 0;
            super.onPause();
            saveState();
        } else {
            super.onPause();
            // Intent intent = new Intent();
            // intent.setClass(JobView_Files_NoteEdit.this,
            // MainMenu_Notepad.class);
            // startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        populateFields();
    }

    private void saveState() {
        // if (Constants.CANCEL_CLICK == 1) {
        // // do nothing
        // Constants.CANCEL_CLICK = 0;
        // } else {
        if (TrafficmanConstants.SAVE_CLICK == 1) {

            String title = mTitleText.getText().toString();
            jv_selected_file_id = 0;

            TrafficmanDateStamp dateStamp = new TrafficmanDateStamp();
            datetime_record_updated = dateStamp.datetime_now.toString();
            jv_filename = TrafficmanConstants.CURRENT_JV_FILENAME;

            if (title.length() > 0) {
                mDbHelper = new TrafficmanFilesNotesDbAdapter(this);
                mDbHelper.open();
                if (mRowId == null) {
                    datetime_record_created = datetime_record_updated;
                    record_id = mDbHelper.createNote(title,
                            jv_selected_file_id, datetime_record_created, datetime_record_updated, jv_filename);
                    if (record_id > 0) {
                        mRowId = record_id;
                    }
                } else {
                    datetime_record_created = TrafficmanConstants.DATETIME_RECORD_CREATED;
                    mDbHelper.updateNote(mRowId, title, jv_selected_file_id,
                            datetime_record_created, datetime_record_updated, jv_filename);
                    TrafficmanConstants.DATETIME_RECORD_CREATED = "";
                }
                mDbHelper.close();
            }
            TrafficmanConstants.SAVE_CLICK = 0;
        } else {
            // do nothing
        }
    }
}
